﻿using System;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace masterview.Code.db
{
    public class DatabaseConnectionHelper
    {
        #region Members

        protected SqlConnection con;
        #endregion

        #region Methods
        protected void SetConnection(String connectionString)
        {
            con = new SqlConnection(connectionString);
        }

        protected void SetLocalDatabaseConnection()
        {
            SetConnection(WebConfigurationManager.ConnectionStrings["LocalDataBase"].ConnectionString);
        }

        protected void SetRemoteDatabaseConnection()
        {
            SetConnection(WebConfigurationManager.ConnectionStrings["ApplicationConfiguration"].ConnectionString);
        }

        /// <summary>
        /// Sets connection string
        /// </summary>
        /// <param name="dataSource">defines Data Source</param>
        /// <param name="initialCatalog">defines Initial Catalog</param>
        /// <param name="persistSecurityInfo">defines Persist Security Info</param>
        /// <param name="userID">defines User ID</param>
        /// <param name="password">defines Password</param>
        protected void SetConnection(string dataSource, string initialCatalog, bool persistSecurityInfo, string userID, string password)
        {
            var csb = new SqlConnectionStringBuilder
                          {
                              DataSource = dataSource,
                              InitialCatalog = initialCatalog,
                              PersistSecurityInfo = persistSecurityInfo,
                              UserID = userID,
                              Password = password
                          };

            if (String.IsNullOrEmpty(csb.ConnectionString)) return;
            Console.Write("csb.ConnectionString() = " + csb.ConnectionString);
            SetConnection(csb.ConnectionString);
        }
        #endregion
    }
}